package com.yzdmw.toutiao.wheeldisk.ui;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.yzdmw.toutiao.R;
import com.yzdmw.toutiao.model.News;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class WheeldiskAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {
    private static final String TAG = "WheeldiskAdapter";
    private static final boolean DEBUG = false;
    private final Context mContext;
    private final List<News> mList;
    private final MyListener mListener;

    public WheeldiskAdapter(Context context, List<News> list, MyListener myListener) {
        this.mContext = context;
        this.mList = list;
        this.mListener = myListener;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @NotNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        if(DEBUG) Log.d(TAG,"onCreateViewHolder");
        return new SimpleViewHolder(inflater,parent);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onBindViewHolder(@NonNull @NotNull RecyclerView.ViewHolder holder, int position) {
        if(DEBUG)
			Log.d(TAG,"onBindViewHolder "+position);
        if (holder instanceof SimpleViewHolder){
            // 设置内容
            SimpleViewHolder simpleViewHolder = (SimpleViewHolder) holder;
            News item = mList.get(position);
            simpleViewHolder.title.setText(item.title);
            simpleViewHolder.image.setImageDrawable(null);
            if (item.image_list != null && !item.image_list.isEmpty()) {
                Glide.with(simpleViewHolder.itemView)
                        .load(item.image_list.get(0).url)
                        .placeholder(R.drawable.ic_baseline_loading)
                        .into(simpleViewHolder.image);
            }
            simpleViewHolder.text_content.setText(item.text_content);
            // 触发点击
            holder.itemView.setOnClickListener(v -> mListener.onItemClick(item));
            // 触发预加载检查
            if (position == mList.size()-2)
                mListener.onPreLoad();
        }
    }

    @Override
    public void onViewRecycled(@NonNull @NotNull RecyclerView.ViewHolder holder) {
        if(DEBUG)
			Log.d(TAG, "onViewRecycled");
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public interface MyListener {
        void onItemClick(News item);
        void onPreLoad();
    }

    public static class SimpleViewHolder extends RecyclerView.ViewHolder {
        TextView title, text_content;
        ImageView image;
        public SimpleViewHolder(LayoutInflater inflater, @NotNull ViewGroup parent) {
            super(inflater.inflate(R.layout.wheeldisk_item_1_1, parent, false));
            title = itemView.findViewById(R.id.title);
            text_content = itemView.findViewById(R.id.text_content);
            image = itemView.findViewById(R.id.image_content);
        }
    }


}
